import pytest

from common.NeqReq import NeqReq
from common.DBManager import DBManager

from utils.yaml_utils import read_yaml
from utils.url_utils import full_url
from utils.data_process_utils import extract_args
from utils.cycle_utils import *


url = "/user/buyTermFinancial"
login_url = '/login/verifyLogin'

data_path = "./data/buy_term_financial.yaml"
pre_path = "./data/setup/buy_term_financial.yaml"
aft_path = "./data/teardown/buy_term_financial.yaml"

class Test_buyTermFinancial:
    
	@pytest.fixture(scope="class",  autouse=True)
	def class_cycle(self):

		dm = DBManager()   
		pre_process(dm, pre_path)
		yield
		print("aft")
		aft_process(dm, aft_path)
		dm.close()


	@pytest.mark.parametrize("args", read_yaml(data_path))
	def test_1(self,args):
        
		nr = NeqReq()

		if not args.get('no_auth'):
			print("登录")
			nr.send_request('GET', full_url(login_url), {
				"username": args['login'],
				"password": 'e10adc3949ba59abbe56e057f20f883e'
			})

		data = extract_args(args, 
			['id','userId','passWord','append'],
			['termFinancialId','userId','passWord','append']
		)

		result = nr.send_request('POST', full_url(url), data)

		assert result['code'] == args['exp1']
		if result['code'] == 200:
			body = result['body']
			assert body.get('code') == args['exp2']

